﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;
using System.Collections.Generic;


public partial class Nha_hang_chi_tiet : System.Web.UI.Page
{
    protected StringBuilder strResult = new StringBuilder();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["NhaHangID"] != null)
        {
            CheckLogin();
            if (!IsPostBack)
            {
                LoadResInfo();
                BindRatingControl();
                GetComment();
                UpdateResView();
                GetResSlide();
                GetAppropriateByResID();
                GetSameLocationRes();
                GetSameStyleResByResID();
                LoadMenu();
            }
        }
        else
            Response.Redirect("~/Error.aspx");
        

    }
    protected void RatingControlChanged(object sender, AjaxControlToolkit.RatingEventArgs e)
    {
        SQLHelper Dal = new SQLHelper();
        Dal.Open();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_UpdateRateValue";
        Dal.ParameterCollection = new string[2] { "@NhaHangID", "@RateValue" };
        Dal.ValueCollection = new string[2] { Request.QueryString["NhaHangID"].ToString(), Convert.ToString(Rating1.CurrentRating) };
        Dal.ExecuteNonQuery();
        BindRatingControl();
    }
    protected void BindRatingControl()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.Open();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetRate";
        Dal.ParameterCollection = new string[1] { "@NhaHangID" };
        Dal.ValueCollection = new string[1] { Request.QueryString["NhaHangID"].ToString() };
        DataRow dr = Dal.GetDataRow();
        int ratevalue = Convert.ToInt32(dr["RateValue"].ToString());
        Rating1.CurrentRating = ratevalue;
    }
    protected void LoadResInfo()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetResInfo";
        Dal.ParameterCollection = new string[1] { "@NhaHangID" };
        Dal.ValueCollection = new string[1] { Request.QueryString["NhaHangID"].ToString() };
        DataRow dr = Dal.GetDataRow();
        Dal.Close();
        Dal.Dispose();
        lblResName.Text = dr["TenNhaHang"].ToString();
        lblViews.Text = dr["LuotXem"].ToString();
        lblComments.Text = dr["LuotBinhLuan"].ToString();
        //lblAddress.Text = dr["DiaChi"].ToString();
        Literal1.Text = dr["BaiGioiThieu"].ToString();
        if (dr["Email"].ToString() != "")
        {
            hplEmail.Text = "Email: " + dr["Email"].ToString();
            hplEmail.NavigateUrl = "mailto:" + dr["Email"].ToString();
        }
        else {
            hplEmail.Visible.Equals(false);
        }
        lblPhone.Text = dr["DienThoai"].ToString();
        lblCapacity.Text = dr["SucChua"].ToString();
        lblUtility.Text = dr["TienIch"].ToString();
        Page.Title ="Phố Ẩm Thực | "+dr["TenNhaHang"].ToString();
        if (!String.IsNullOrEmpty(dr["Facebook"].ToString()))
        {
            hplFacebook.Text = "Facebook: " + dr["FaceBook"].ToString();
            hplFacebook.NavigateUrl = "https://www." + dr["FaceBook"].ToString();
        }
        else {
            hplFacebook.Visible.Equals(false);
        }
        if (!String.IsNullOrEmpty(dr["Website"].ToString()))
        {
            hplWebsite.Text = "Website: " + dr["WebSite"].ToString();
            hplWebsite.NavigateUrl = dr["WebSite"].ToString();
        }
        else {
            hplWebsite.Visible.Equals(false);
        }
    }
    protected void GetComment()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetResCommentByResID";
        Dal.ParameterCollection = new string[1] { "@NhaHangID" };
        Dal.ValueCollection = new string[1] { Request.QueryString["NhaHangID"].ToString() };
        DataTable db = Dal.GetDataTable();
        if (db != null)
        {
            Repeater1.DataSource = db;
            Repeater1.DataBind();
        }
        Dal.Close();
        Dal.Dispose();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SQLHelper Dal = new SQLHelper();
        Dal.Open();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_AddResComment";
        Dal.ParameterCollection = new string[4] { "@NhaHangID", "@UserEmail", "@NoiDung", "@NgayDang" };
        Dal.ValueCollection = new string[4] { Request.QueryString["NhaHangID"].ToString(), Session["UserEmail"].ToString(), TextBox1.Text, DateTime.Today.ToString() };
        Dal.ExecuteNonQuery();
        Dal.Close();
        Dal.Dispose();
        TextBox1.Text = "";
        GetComment();
    }
    protected void CheckLogin()
    {
        if (Session["UserEmail"] == null)
        {
            Button1.Enabled = false;
            Button1.ToolTip = "Bạn cần đăng nhập để bình luận";
            TextBox1.Text = "Bạn cần đăng nhập để bình luận";
        }

    }
    
    protected void UpdateResView()
    {
        //try
        //{
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_Update_ResViews";
            Dal.ParameterCollection = new string[1] { "NhaHangID" };
            Dal.ValueCollection = new string[1] { Request.QueryString["NhaHangID"].ToString() };
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
        //}
        //catch
        //{
        //    Response.Redirect("Error.aspx");
        //}
    }
    protected void GetResAppropriate()
    {
        //try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAppropriateByResID";
            Dal.ParameterCollection = new string[1] { "@NhaHangID" };
            Dal.ValueCollection = new string[1] { Request.QueryString["NhaHangID"].ToString() };
            DataTable db = Dal.GetDataTable();
            rptAppropriate.DataSource = db;
            rptAppropriate.DataBind();
        //}
        //catch { }
    }
    protected void GetResSlide()
    {
        //try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetResSlideByResID";
            Dal.ParameterCollection = new string[1] { "@NhaHangID"};
            Dal.ValueCollection = new string[1] { Request.QueryString["NhahangID"].ToString()};
            DataTable db = Dal.GetDataTable();
            rptSlideImage.DataSource = db;
            rptSlideImage.DataBind();
        //}
        //catch { }
    }
    protected void GetAppropriateByResID()
    {
        //try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAppropriateByResID";
            Dal.ParameterCollection = new string[1] { "@NhaHangID"};
            Dal.ValueCollection = new string[1] { Request.QueryString["NhaHangID"].ToString()};
            DataTable db = Dal.GetDataTable();
            rptAppropriate.DataSource = db;
            rptAppropriate.DataBind();
        //}
        //catch { }
    }
    protected void GetSameLocationRes()
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetSameLocationRes";
            Dal.ParameterCollection = new string[1] { "@NhaHangID"};
            Dal.ValueCollection = new string[1] { Request.QueryString["NhaHangID"].ToString() };
            DataTable db = Dal.GetDataTable();
            rptSameLocation.DataSource = db;
            rptSameLocation.DataBind();
        }
        catch { }
    }
    protected void GetSameStyleResByResID()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetSameStyleResByResID";
        Dal.ParameterCollection = new string[1] { "@NhaHangID"};
        Dal.ValueCollection = new string[1] { Request.QueryString["NhaHangID"].ToString()};
        DataTable db = Dal.GetDataTable();
        rptSameStyle.DataSource = db;
        rptSameStyle.DataBind();
    }
    protected void LoadMenu()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_LoaiMonByResID";
        Dal.ParameterCollection = new string[1] { "@NhaHangID" };
        Dal.ValueCollection = new string[1] { Request.QueryString["NhaHangID"].ToString() };
        DataTable db1 = Dal.GetDataTable();
        SQLHelper Dal2 = new SQLHelper();
        Dal2.Open();
        Dal2.CommandType = CommandType.StoredProcedure;
        Dal2.CommandText = "SP_GetMenuByResID";
        Dal2.ParameterCollection = new string[1] { "@NhaHangID" };
        Dal2.ValueCollection = new string[1] { Request.QueryString["NhaHangID"].ToString() };
        DataTable db2 = Dal2.GetDataTable();
            strResult = new StringBuilder();
            for (int i = 0; i < db1.Rows.Count; i++)
            {
                strResult.Append("<div class=\"LoaiMon\">"+db1.Rows[i]["TenLoaiMon"].ToString()+"</div>");
                int k = 0;
                for (int j = 0; j < db2.Rows.Count; j++)
                {
                    
                    if (db1.Rows[i]["LoaiMonID"].ToString() == db2.Rows[j]["LoaiMonID"].ToString())
                    {
                        k = k + 1;
                        strResult.Append("<div class=\"clearfix\" style=\"padding-top:5px;padding-bottom:5px\">");
                        strResult.Append("<div class=\"TenMon\">" + "<span class=\"SoThuTu\">" + (k) + "</span>" + db2.Rows[j]["TenMon"].ToString() + "</div>");
                        strResult.Append("<div class=\"DonGia\">"+db2.Rows[j]["DonGia"].ToString()+"<span> VNĐ</span>"+"</div>");
                        strResult.Append("</div>");
                    }

                }
            }


    }

    protected void rptLoaiMon_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        

    }
}
